package com.shopowner.service.impl;


import com.shopowner.dao.FieldDao;
import com.shopowner.dto.DTO;
import com.shopowner.dto.MessageEnum;
import com.shopowner.entity.Field;
import com.shopowner.page.PageInfo;
import com.shopowner.service.FieldService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("fieldService")
public class FieldServiceImpl implements FieldService {
    @Resource
    FieldDao fieldDao;
    @Override
    public DTO<Field> getAllFields()  throws Exception{
        List<Field> fields = fieldDao.getAllFields();
        MessageEnum messageEnum  = Optional.ofNullable(fields).isPresent()?MessageEnum.GETADMINSUCCESS: MessageEnum.GETADMINFAIL;
        return new DTO<Field>(messageEnum.getCode(),messageEnum.getMessage(),fields);
    }

    @Override
    public DTO<Field> deleteFieldByFno(String fno)  throws Exception{
        Integer result = fieldDao.deleteFieldByFno(fno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<Field>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Field> selectFieldByFno(String fno)  throws Exception{
        Field fields = fieldDao.selectFieldByFno(fno);
        MessageEnum messageEnum  = Optional.ofNullable(fields).isPresent()?MessageEnum.GETPARENTSSUCCESS: MessageEnum.GETPARENTSFAIL;
        return new DTO<Field>(messageEnum.getCode(),messageEnum.getMessage(),fields);
    }
    @Override
    public DTO<Field> updateFieldByFno(Field field)  throws Exception{
        Integer result = fieldDao.updateFieldByFno(field);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<Field>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Field> addField(Field field) throws Exception {
        Integer result = fieldDao.addField(field);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<Field>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<Field> getFieldByPage(PageInfo pageInfo ) throws Exception {
        List<Field> fields = fieldDao.getFieldByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(fields).isPresent()?MessageEnum.GETADMINSUCCESS: MessageEnum.GETADMINFAIL;
        return new DTO<Field>(messageEnum.getCode(),messageEnum.getMessage(),fields,pageInfo);
    }


}
